//
//  VowViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/11/8.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "VowViewController.h"
#import "UIView+Extension.h"
#import "VowListViewController.h"
#import "ZJClassifyViewController.h"
#import "SubmitOrderViewController.h"
@interface VowViewController ()<UITextViewDelegate>
@property (nonatomic,strong)UIScrollView*mainScroView;
@property (assign, nonatomic) CGFloat   offetHeight;
@property (nonatomic ,strong) UITextView*textView;
@property (nonatomic ,strong) NSMutableArray*listArray;
@property (nonatomic ,strong) NSMutableArray*resultArray;
@property (nonatomic ,strong) UIView *plusView;
@property (nonatomic ,strong) UIImageView *imageView;
@property (nonatomic ,assign) BOOL isPublic;
@property (nonatomic ,assign) BOOL isMatch;
@property (nonatomic ,assign) BOOL isShow;
@property (nonatomic ,strong) UIButton *lastBtn;


@end

@implementation VowViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.shopModle!=nil) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.shopModle.imageUrl]];
        NSLog(@"曹尼玛");
    }
}
-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray=[NSMutableArray array];
        
    }
    return _listArray;
}
-(NSMutableArray *)resultArray{
    if (!_resultArray) {
        _resultArray=[NSMutableArray array];
        
    }
    return _resultArray;
}
-(UIScrollView*)mainScroView{
    if (!_mainScroView) {
        _mainScroView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64-49)];
        _mainScroView.backgroundColor = Gray_Color;
        _mainScroView.contentSize = CGSizeMake(0, MainH-64-49);
        _mainScroView.showsHorizontalScrollIndicator = NO;
        _mainScroView.showsVerticalScrollIndicator = NO;
        
    }
    return _mainScroView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[self mainScroView]];
    [self loadRequest];
    // Do any additional setup after loading the view.
}
-(void)creatView:(NSArray *)sortArray{
    //选择类别
    UIView*btnView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 200)];
    btnView.backgroundColor=[UIColor whiteColor];
    [self.mainScroView addSubview:btnView];
	
	
	//单击的手势
	UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
	tapRecognize.numberOfTapsRequired = 1;
//	tapRecognize.delegate = self;
	[tapRecognize setEnabled :YES];
	[tapRecognize delaysTouchesBegan];
	[tapRecognize cancelsTouchesInView];
	
	[self.mainScroView addGestureRecognizer:tapRecognize];
    
    MYLabel *label=[[MYLabel alloc]initWithFrame:CGRectMake(10, 20, MainW-20, 15)];
    label.text=MyString(@"请选择类别");
    label.textColor=TextColor3;
    [btnView addSubview:label];
    int count = 0;
    float btnWidth = 0;
    float viewHeight = 0;
    for (int i = 0; i < sortArray.count; i++) {
        
        NSString *btnName = sortArray[i][@"name"];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundColor:BackgroundColor ];
        [btn setTitleColor:TextColor6 forState:UIControlStateNormal];
        [btn setTitleColor:Red_Color forState:UIControlStateSelected];
        
        btn.titleLabel.font = FontAndStyle12;
        [btn setTitle:btnName forState:UIControlStateNormal];
        //btn.titleLabel.lineBreakMode =  NSLineBreakByWordWrapping;
        [btn addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        ViewRadius(btn, 2);
        //NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(12) forKey:NSFontAttributeName];
        CGSize btnSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font }];
        
        btn.width = btnSize.width + 10;
        btn.height = btnSize.height + 10;
        
        if (i==0)
        {
            btn.x = 10;
            btnWidth = CGRectGetMaxX(btn.frame);
        }
        else{
            btnWidth += CGRectGetMaxX(btn.frame)+10;
            if (btnWidth > MainW) {
                count++;
                btn.x = 10;
                btnWidth = CGRectGetMaxX(btn.frame);
            }
            else{
                
                btn.x += btnWidth - btn.bsg_width;
            }
        }
        btn.y += count * (btn.bsg_height+10)+50;
        
        viewHeight = CGRectGetMaxY(btn.frame)+10;
        
        [btnView addSubview:btn];
        
        btn.tag = i;
        
        
        //        if ([btnName isEqualToString:self.selectStr])
        //        {
        //            self.selectBtn = btn;
        //            self.selectBtn.selected = YES;
        //            self.selectBtn.backgroundColor = [UIColor greenColor];
        //        }
        
    }
    if (viewHeight==0) {
        btnView.bsg_height=50;
    }else{
        btnView.bsg_height=viewHeight+10;
    }
   
    //心愿
    UIView*vowView=[[UIView alloc]initWithFrame:CGRectMake(0, btnView.bsg_bottom+10, MainW, 200)];
    vowView.backgroundColor=[UIColor whiteColor];
    [self.mainScroView addSubview:vowView];
    
    MYLabel *label1=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, MainW-20, 20)];
    label1.text=MyString(@"请在这里写下您的心愿");
    label1.textColor=TextColor3;
    [vowView addSubview:label1];
    
    UIView*line=[[UIView alloc]initWithFrame:CGRectMake(0, label1.bsg_bottom+15, MainW, 1)];
    line.backgroundColor=Gray_Color;
    [vowView addSubview:line];
    
    
    self.textView=[[UITextView alloc]initWithFrame:CGRectMake(20, line.bsg_bottom+20, MainW-40, 100)];
    self.textView.delegate=self;
    self.textView.text=MyString(@"输入您的心愿!");
    self.textView.textColor=TextColor9;
    [vowView addSubview:self.textView];
    
    
    UIView*line1=[[UIView alloc]initWithFrame:CGRectMake(0, self.textView.bsg_bottom+15, MainW, 1)];
    line1.backgroundColor=Gray_Color;
    [vowView addSubview:line1];


    MYLabel *label2=[[MYLabel alloc]initWithFrame:CGRectMake(20, line1.bsg_bottom+15, MainW-80, 20)];
    label2.text=MyString(@"是否公开心愿");
    label2.textColor=TextColor3;
    [vowView addSubview:label2];
    UIButton*btn=[[UIButton alloc]init];
    btn.frame=CGRectMake(MainW-40, line1.bsg_bottom+10, 30, 30);
    [btn setImage:[UIImage imageNamed:@"icon_rb_select"] forState:UIControlStateSelected];
    [btn setImage:[UIImage imageNamed:@"icon_rb_nor"] forState:UIControlStateNormal];
    btn.selected=YES;
    self.isPublic=btn.selected;
    btn.tag=100;
    [btn addTarget:self action:@selector(chooseclick:) forControlEvents:UIControlEventTouchUpInside];
    [vowView addSubview:btn];
    
    
    vowView.bsg_height=CGRectGetMaxY(label2.frame)+15;
    //是否加持
    UIView*plusView=[[UIView alloc]initWithFrame:CGRectMake(0, vowView.bsg_bottom+10, MainW, 50)];
    plusView.backgroundColor=[UIColor whiteColor];
    
    [self.mainScroView addSubview:plusView];
    MYLabel *label3=[[MYLabel alloc]initWithFrame:CGRectMake(10, 15, MainW-80, 20)];
    label3.text=MyString(@"是否选择加持");
    label3.textColor=TextColor3;
    [plusView addSubview:label3];
    
    UIButton*btn1=[[UIButton alloc]init];
    btn1.frame=CGRectMake(MainW-40, 10, 30, 30);
    [btn1 setImage:[UIImage imageNamed:@"icon_rb_select"] forState:UIControlStateSelected];
    [btn1 setImage:[UIImage imageNamed:@"icon_rb_nor"] forState:UIControlStateNormal];
    btn1.selected=YES;
    self.isShow=YES;
    btn1.tag=200;
    [btn1 addTarget:self action:@selector(chooseclick:) forControlEvents:UIControlEventTouchUpInside];
    [plusView addSubview:btn1];
    
    
    
    //选择图片
    UIView*chooseView=[[UIView alloc]initWithFrame:CGRectMake(0, plusView.bsg_bottom+1, MainW, 500)];
    chooseView.backgroundColor=[UIColor whiteColor];
    self.plusView=chooseView;
    [self.mainScroView addSubview:chooseView];
    
    UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(MainW/2-40, 20, 80, 80)];
    image.image=[UIImage imageNamed:@"img_fail_monk"];
    self.imageView=image;
    [chooseView addSubview:image];
    UIButton*readyBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    readyBtn.frame=CGRectMake(MainW/2-90, image.bsg_bottom+20, 80, 25);
    [readyBtn setTitle:MyString(@"选择已请") forState:0];
    readyBtn.tag=10;
    [readyBtn addTarget:self action:@selector(matchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [readyBtn setTitleColor:TextColor6 forState:0];
    readyBtn.backgroundColor=Gray_Color;
    ViewRadius(readyBtn, 2);
    [chooseView addSubview:readyBtn];
    
    UIButton*matchBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    matchBtn.frame=CGRectMake(MainW/2+10, image.bsg_bottom+20, 80, 25);
    [matchBtn setTitle:MyString(@"选择匹配") forState:0];
    matchBtn.backgroundColor=Gray_Color;
    [matchBtn setTitleColor:TextColor6 forState:0];
    ViewRadius(matchBtn, 2);
    
    //matchBtn.layer.borderWidth=1;
     matchBtn.tag=20;
    //matchBtn.layer.borderColor=Red_Color.CGColor;
    [matchBtn addTarget:self action:@selector(matchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [chooseView addSubview:matchBtn];
    
    chooseView.bsg_height=CGRectGetMaxY(matchBtn.frame)+20;
    
    
    
   //_mainScroView.contentSize = CGSizeMake(0, MainH-64-49);
    self.mainScroView.contentSize=CGSizeMake(0, CGRectGetMaxY(chooseView.frame)+10);
    
    
    UIButton*determineBtn=[UIButton buttonWithType:UIButtonTypeSystem];
    determineBtn.frame=CGRectMake(0, MainH-49-64, MainW , 49);
    [determineBtn setTitle:@"提交" forState:0];
    [determineBtn setTitleColor:[UIColor whiteColor] forState:0];
    determineBtn.backgroundColor=Red_Color;
    [determineBtn addTarget:self action:@selector(userprayaddClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:determineBtn];
    
}
-(void)fourBtnClick:(UIButton*)sender{
    NSString *oppitionId=self.listArray[sender.tag][@"optionId"];
    sender.selected=!sender.selected;
    if (sender.selected==YES) {
        [sender setBackgroundColor:[UIColor whiteColor]];
        sender.layer.borderWidth=1;
        sender.layer.borderColor=button_Red_Color.CGColor;
        [self.resultArray addObject:oppitionId];
    }else{
        [sender setBackgroundColor:HJ_Color(229, 229, 229)];
        sender.layer.borderWidth=0.01;
        sender.layer.borderColor=[UIColor whiteColor].CGColor;
        [self.resultArray removeObject:oppitionId];
    }
    NSLog(@"选择了多少个---%@",self.resultArray);
}
-(void)loadRequest{
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    [self doHttpRequestWithApi:buddhaattrlist method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.listArray =dic[@"data"];
        [self creatView:dic[@"data"]];
    }];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = MyString(@"输入您的心愿!");
        textView.textColor = TextColor9;
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:MyString(@"输入您的心愿!")]){
        textView.text=@"";
        textView.textColor=TextColor3;
    }
}
-(void)chooseclick:(UIButton*)sender{
    sender.selected=!sender.selected;
    if (sender.tag==200) {
        if (sender.selected) {
            self.plusView.hidden=NO;
            self.isShow=YES;
        }else{
            self.plusView.hidden=YES;
            self.isShow=NO;;
        }
    }else{
        self.isPublic=sender.selected;
    }
}
-(void)subMitRequest{
    NSMutableDictionary*dicc=[NSMutableDictionary dictionary];
    dicc[@"userId"]      =[ZJUserDefaultsTools getCurrentUserID];
    
    dicc[@"prayInfo"]    =self.textView.text;
    dicc[@"attroptions"] =[self.resultArray mj_JSONString];
    if (self.isPublic==YES) {
        dicc[@"isPublic"]    =@"1";
    }else{
        dicc[@"isPublic"]    =@"0";
    }
    if (self.isShow==YES) {
        if (self.isMatch==YES) {
            dicc[@"isMatch"]    =@"0";
        }else{
            dicc[@"isMatch"]    =@"1";
        }
        dicc[@"skuId"]       =self.shopModle.skuId;
    }else{
        self.isMatch=YES;
        dicc[@"isMatch"]    =@"";
        dicc[@"skuId"]       =@"";
    }
    if (self.isMatch==NO) {
        LXAlertView*lxalert=[[LXAlertView alloc]initWithTitle:nil message:MyString(@"心愿提示") cancelBtnTitle:@"取消" otherBtnTitle:@"确定" clickIndexBlock:^(NSInteger clickIndex) {
            if (clickIndex==1) {
                NSMutableDictionary *dic=[[NSMutableDictionary   alloc]init];
                dic[@"skuId"]=self.shopModle.skuId;
                dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
                dic[@"number"]=@"1";
                [self doHttpRequestWithApi:API_SHOPBUYNOW_LIST method:POST dic:dic successBlock:^(NSDictionary *dic) {
                    NSLog(@"%@",dic);
                    SubmitOrderViewController*vc=[[SubmitOrderViewController alloc]init];
                    vc.Praydic=dicc;
                    vc.orderDic=dic[@"data"];
                    vc.styple=Supplication;
                    [self.navigationController pushViewController:vc animated:YES];
                }];
            }
        }];
        [lxalert showLXAlertView];
        return;
        
    }
    
    
    [self doHttpRequestWithApi:userprayadd method:GET dic:dicc successBlock:^(NSDictionary *dic) {
        [MBProgressHUD showSuccess:@"祈愿成功!"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    }];
}
-(void)userprayaddClick{
    if (self.resultArray.count==0) {
        [MBProgressHUD showError:@"请选择类别!"];
        return;
    }
    if([self.textView.text isEqualToString:MyString(@"输入您的心愿!")]){
        [MBProgressHUD showError:@"请输入您的心愿!"];
        return;
    }
    if (self.isShow==YES) {
        if (self.shopModle==nil) {
            [MBProgressHUD showError:@"请选择佛像进行加持或者不加持心愿!"];
            return;
        }
    }
    [self subMitRequest];
}
-(void)matchBtnClick:(UIButton *)sender{
    if (self.lastBtn!=sender) {
        [sender setTitleColor:Red_Color forState:0];
        sender.backgroundColor=[UIColor whiteColor];
        ViewRadius(sender, 2);
        sender.layer.borderWidth=1;
        sender.layer.borderColor=Red_Color.CGColor;
        [self.lastBtn setTitleColor:TextColor6 forState:0];
        self.lastBtn.backgroundColor=Gray_Color;
        self.lastBtn.layer.borderWidth=0.01;
        self.lastBtn=sender;
    }
   
    
    if (sender.tag==10) {
        VowListViewController*vc=[[VowListViewController alloc]init];
        vc.isMatch=YES;
        self.isMatch=YES;
        vc.popWhere=3;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        if (self.resultArray.count==0) {
            [MBProgressHUD showError:@"请选择类别!"];
            return;
        }
        VowListViewController*vc=[[VowListViewController alloc]init];
        vc.prayString=[self.resultArray mj_JSONString];
        vc.isMatch=NO;
        vc.popWhere=3;
        self.isMatch=NO;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma UIGestureRecognizer Handles
-(void) handleTap:(UITapGestureRecognizer *)recognizer
{
	NSLog(@"---单击手势-------");
	[self.view endEditing:YES];

}
@end
